{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit uProfilCreation;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Spin, modLangGetList, Menus;

type
  TfrmProfilCreation = class(TForm)
    cmdProfilCreer: TButton;
    GroupBox1: TGroupBox;
    txtPseudo: TEdit;
    lblProfilNewPseudo: TLabel;
    lblProfilNewPassword: TLabel;
    txtProfilPassword: TEdit;
    seProfilNewUnivers: TSpinEdit;
    lblProfilNewUnivers: TLabel;
    lblProfilNewServeur: TLabel;
    cbServeurProfil: TComboBox;
    cmdCancel: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cmdProfilCreerClick(Sender: TObject);
  private
    { Private declarations }
    FLanguageList : TScanLanguage;

  public
    { Public declarations }
    NewProfilFile: string;

    procedure ClearForm;
    procedure RefreshLangCombo;
  end;


  
var
  frmProfilCreation: TfrmProfilCreation;

implementation

{$R *.dfm}

uses modProfilsForAttack,
     gnugettext,
     modConstantes;

procedure TfrmProfilCreation.FormCreate(Sender: TObject);
begin
   FLanguageList := TScanLanguage.Create;

   RefreshLangCombo;

    // traduction, car la form est créé sur le vol
    TranslateComponent(Self);

end;

procedure TfrmProfilCreation.FormDestroy(Sender: TObject);
begin
    FLanguageList.Free;

end;

procedure TfrmProfilCreation.RefreshLangCombo;
var i: integer;
    pe : PLangResult;
    iIndex: integer;

    xfile: string;
begin
    FLanguageList.ScanDir(Global_DossierLang);

    iIndex := 0;

    cbServeurProfil.Clear;
    cbServeurProfil.Items.Add('Français (ogame.fr) (sans fichier langue)');

    if FLanguageList.Count > 0 then begin
       for i := 0 to FLanguageList.Count - 1 do begin
           pe := FLanguageList.Items[i];

           cbServeurProfil.Items.Add(pe.Nom);

           xfile := ExtractFileName(pe.Fichier);
           if SameText(xfile, 'fr.ini')  then begin
              iIndex := cbServeurProfil.Items.Count - 1;
           end;
       end;
    end;

    cbServeurProfil.ItemIndex := iIndex;
end;

procedure TfrmProfilCreation.ClearForm;
begin
    txtPseudo.Text := '';
    txtProfilPassword.Text := '';
end;


procedure TfrmProfilCreation.cmdProfilCreerClick(Sender: TObject);
var ProfilNew: TProfilsAttack;
    Nick, Fichier: string;
    pe : PLangResult;
begin

    Nick := trim(txtPseudo.Text);

    if Nick = '' then exit;

    ProfilNew := TProfilsAttack.Create;

    ProfilNew.Pseudo := Nick;
    ProfilNew.Univers := seProfilNewUnivers.Value;
    ProfilNew.Password := txtProfilPassword.Text;

    if cbServeurProfil.ItemIndex > 0 then begin
       // on utilise un fichier de langue

       if (FLanguageList.Count + 1) > cbServeurProfil.ItemIndex  then begin
           // le premier element n'est pas present dans FLanguageList
           pe := FLanguageList.Items[cbServeurProfil.ItemIndex - 1];
           ProfilNew.LangageUni := ExtractFileName(pe.Fichier);

       end else begin
           // erreur, impossible...
           ProfilNew.LangageUni := LANG_INTERNAL;
       end;
       

    end else begin
       ProfilNew.LangageUni := LANG_INTERNAL;
    end;


    NewProfilFile := format('%2.2d-%s.ini', [seProfilNewUnivers.Value, Nick]);


    ForceDirectories(Global_DossierProfils);
    Fichier := Global_DossierProfils + NewProfilFile;

    ProfilNew.SetDefaultValue;

    ProfilNew.SaveToFile(Fichier);

    ProfilNew.Free;


    // La fonction Close met ModalResult à mrCancel
    Close;

    // On modifie alors en mrOK
    ModalResult := mrOK;
end;

end.
